@extends("layout.app")

@section("content")

    <div class="form-inline">

        <div class="form-group has-success">
            <input class="form-control" id="begin_time" placeholder="开始时间"/>
        </div>
        <div class="form-group has-success">
            <input class="form-control" id="end_time" placeholder="结束时间"/>
        </div>
        <div class="form-group has-success">
            &nbsp;<button id="button" class="btn btn-default" onclick="jQuery('#rowed3').setGridParam({postData: postData()}).trigger('reloadGrid')"><i class="fa fa-search"></i> 查询</button>
        </div>
        <div class="form-group has-success">
            &nbsp;<button id="button" class="btn btn-success" onclick="setDate('<?php echo date("Y-m-d", strtotime("Monday this week"))?>', '')"> 本周充值</button>
        </div>
        <div class="form-group has-success">
            &nbsp;<button id="button" class="btn btn-info" onclick="setDate('<?php echo date("Y-m-d", strtotime("Monday this week")-604800)?>', '<?php echo date("Y-m-d", strtotime("last Sunday"))?>')"> 上周充值</button>
        </div>
        <div class="form-group has-success">
            &nbsp;<button id="button" class="btn btn-warning" onclick="setDate('<?php echo date("Y-m-01")?>', '')"> 本月充值</button>
        </div>
        <div class="form-group has-success">
            &nbsp;<button id="button" class="btn btn-danger" onclick="setDate('<?php echo date("Y-m-01",  strtotime(date("Y-m-01") . " -1 month"))?>', '<?php echo date("Y-m-d",  strtotime(date("Y-m-01") . " -1 day"))?>')"> 上月充值</button>
        </div>
    </div>
    <div class="separator"></div>
    <div class="box-body">

        <table id="rowed3"></table>
        <div id="prowed3"></div>

    </div>
<script>

$(document).ready(function() {

	var data = {
        url :"/agent/zongdai",
        postData: postData(),
        colNames: ["用户ID", "昵称",  "账户余额", "合伙人", "玩家人数", "合伙人比例", "伙伴比例(%)", "收益明细", "备注", "操作"],
        colModel: [
            {
                name: "id",
                editable: true,
                key: true,
                @if($chaoz)
                editoptions:{"disabled":"disabled"}
                @endif
            }, {
                name: "nickname",
                sortable: false,
                search: false
            }, {
                name: "money",
                sortable: false,
                search: false,
                formatter: function(cellvalue, options, object) {
                    return cellvalue/100;
                }
            },{
                name: 'chao_zongdai',
                stype: 'select',
                searchoptions: {
                    sopt: ["eq" , "ne"],
                    value: "{{$rs}}"
                },
                editable: true,
                edittype: 'select',
                editoptions: {
                    value: "{{$rs}}"
                },
                formatter: function(cellvalue, options, object) {
                    return object.rs;
                }
            }, {
                name: "c1",
                sortable: false,
                search: false,
                formatter: function(cellvalue, options, object) {
                    return '<a href="/user?zongdai='+object.id+'"><span class="text-success">'+cellvalue+'</span></a>';
                }
            }, {
                name: "chao_ratio",
                sortable: false
            }, {
                name: "zong_ratio",
                sortable: false,
                editable: true
            }, {
                name: "id",
                sortable: false,
                search: false,
                formatter: function(cellvalue, options, object) {
                	return '<a href="/agent/zongdai/detail?zongdai='+object["id"]+'"><i class="fa fa-link text-success"></i></a>';
                }
            }, {
                name: "desc",
                sortable: false,
                search: false,
                editable: true,
                edittype:'textarea',
                formoptions:{"elmsuffix":"<br><p class='text-danger' style='position: absolute;margin-top:5px'>(设置伙伴比例下一小时整点生效,50-59分不能设置)</p>"}
            },<?php if ($chaoz):?>{
                name: "id",
                sortable: false,
                search: false,
                formatter: function(cellvalue, options, object) {
                    return '<a href="javascript:;" onclick="verify('+object.id+','+object.money/100+','+object.cm+')" data-target="#tranModal"><span class=" text-danger">转账</span></a>';
                    }
            }<?php else:?>{
                name: "id",
                hidden: true,
            }<?php endif;?>],
        caption: '<i class="fa fa-list"></i> {{$label2}}',
        sortname: "money",
        sortorder: "desc",
        editurl: "/agent/zongdai/edit",
        datatype: "json",
        pager: "#prowed3",
        height:'auto',
        autowidth: true,
        viewrecords:true,
        rowList:[20,40,60],

        // loadonce:true,
    };

	var add = true;
	@if($chaoz)
    add = false;
	@endif
	// jqGrid(data, true, add, true);
    $('#rowed3').jqGrid(data).navGrid("#prowed3", {edit: true,add: add,search:true,del: true,refresh:true},
    {
        closeAfterEdit: false,
        beforeShowForm: function(e) {
            $('#FrmGrid_rowed3').height(200);
            $('#editmodrowed3').width(400);
        },
        afterSubmit: function(resp) {

            var jsonResponse = $.parseJSON(resp.responseText);

            if (!jsonResponse.success) {
                $.unblockUI();
                return [false, jsonResponse.message];
            }
            $.gritter.add({
                text: jsonResponse.message,
                sticky: false,
                time: '',
                class_name: 'gritter-light'
            });

            $(".ui-widget-overlay").click();
            return [true, resp.responseText];
        }

    }, {
        closeAfterAdd: false,
        afterSubmit: function(resp) {

            var jsonResponse = $.parseJSON(resp.responseText);

            if (!jsonResponse.success) {
                $.unblockUI();
                return [false, jsonResponse.message];
            }

            $.gritter.add({
                text: '添加数据成功',
                sticky: false,
                time: '',
                class_name: 'gritter-light'
            });

            $(".ui-widget-overlay").click();
            return [true, resp.responseText];
        }
    },{},{closeAfterSearch: true,
           closeOnEscape: true,multipleSearch: true});

    $(".form-inline .form-control").datepicker({
        dateFormat: 'yy-mm-dd',
        monthNames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
        dayNamesMin: ['日','一','二','三','四','五','六'] });

});

var loader = "<div style='text-align:center'><img src='/img/loaders/15.gif'></div>";

function setDate(begin_time, end_time) {

    $("#begin_time").val(begin_time);
    $("#end_time").val(end_time);

    jQuery('#rowed3').setGridParam({postData: postData()}).trigger('reloadGrid')
}

function postData() {
    return {
    	chao_zongdai: '<?php echo $request->get("chao_zongdai")?>',
        begin_time: $("#begin_time").val(),
        end_time: $("#end_time").val()
    }
}

function check(id) {
    $("#checkSpan").append("请设置下属用户" + id + "的总分成比例");
	$("input[name=id]").val(id);
}

function verify(id, zongdai_money,cm) {
	$("#trans_money").next().remove();
	$("#notice").hide();
	$("#fh-button").attr("disabled",false);
	$("#fh-cancel").attr("disabled",false);
	$("#fhModal").modal([]);
	$("#diamondModalLabel").html("向[" + id + "]转账,<?php if($money !== ""):?>(保险箱余额:<span class='text-danger'>"+cm+"元</span>)<?php endif;?>");
	$("#zongdai_id").val(id);
	$("#zongdai_money").val(zongdai_money);
}

function transfer(zongdai_id, zongdai_money, trans_money, desc) {
	<?php if($chaoz):?>

	var chaoz_id = <?php echo $chaoz->id?>;
	var chaoz_money = <?php echo $chaoz->money2/100?>;
	<?php else:?>
		return false;
	<?php endif; ?>

	if(trans_money < 0 && trans_money < -(zongdai_money)){
		$("#notice").show();
	} else if(trans_money > 0 && trans_money > chaoz_money){
		$("#notice").show();
	} else if (trans_money == 0){
		$("#notice").show();
	} else {
		$("#notice").hide();
		$("#fh-button").attr("disabled","disabled");
		$("#fh-cancel").attr("disabled","disabled");
		$("#trans_money").after(loader);
		$.post("/agent/zongdai/transfer",{zongdai_id: zongdai_id, trans_money: trans_money, desc: desc}, function(result){
			if (result.success) {
				$("#rowed3").trigger("reloadGrid");
		        $.gritter.add({
		            text:result.message,
		            sticky: false,
		            time: '',
		            class_name: 'gritter-light'});

			} else {
				 $.gritter.add({
			        title: '错误信息',
			        text:result.message,
			        sticky: false,
			        time: ''});
			}
			$("#fhModal").modal('hide');
		}).error(function(err){
			$("#trans_money").next().remove();
			$("#fh-button").attr("disabled",false);
			$("#fh-cancel").attr("disabled",false);
			$.gritter.add({
		        title:  '错误信息',
		        text:   err,
		        sticky: false,
		        time:   ''});
			});
	}
}

</script>

<div class="modal fade" id="fhModal" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="fhModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
			<div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-name="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="diamondModalLabel"></h4>
            </div>
            <div class="modal-body">
            	<input type="hidden" id="zongdai_id" />
            	<input type="hidden" id="zongdai_money" />
                <div class="form-group">
                    <label> 转账 <small class="text-danger">(转账2分钟后开始生效)</small></label>
                    <input name="diamond" id="trans_money" class="form-control" type="number" min=0 max=99999 />
                </div>
                <div class="form-group">
                    <label> 备注</label>
                    <input name="desc" id="desc" class="form-control" type="text" />
                </div>
				<label id="notice" class="text-danger" style="display:none;">转账数量有误,请重新输入</label>
            </div>
            <div class="modal-footer">
                <button id="fh-button" type="button" class="btn btn-success" onclick="transfer($('#zongdai_id').val(), $('#zongdai_money').val(), $('#trans_money').val(), $('#desc').val())"><i class="fa fa-lock"> 确定</i></button>
                <button id="fh-cancel" type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="chModal" tabindex="-1" role="dialog" aria-labelledby="chModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title"><span id="checkSpan" class="text-danger"></span></h4>
            </div>
            <div class="modal-body">
                <form id="fencheng-form" method="post" action="/zong/ratio">
                    {!! csrf_field() !!}
                    <input type="hidden" name="id" />
                    <div class="radio">
                        <label style="display:none;">
                            <input type="radio" name="ratio" id="optionsRadios1" value="type1" checked>
                            <h4>总比例:</h4>
                        </label>
                            <input type="number" class="form-control" placeholder="请设置1-100之间的整数" name="ratio" value="" /><br/>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button id="pw-button" type="submit" class="btn btn-success" onclick="$('#fencheng-form').submit();return false;"><i class="fa fa-check"></i> 确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

@endsection